package com.hepinghospital.user.ui.activity.nursing.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.hepinghospital.user.R
import com.hepinghospital.user.adapter.NurseConsultAdapter
import com.hepinghospital.user.base.BaseFragment
import com.hepinghospital.user.ui.dialog.ChooseDialog
import com.hepinghospital.user.ui.dialog.ConsultEvaluateDialog
import com.hepinghospital.user.util.ToastUtil
import com.rest.business.RestProxy
import com.rest.observer.BaseObserver
import com.rest.response.FindPatientOrdersResponse
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout


/**
 * @Description
 * @Author WuJianFeng
 * @Date 2022/8/9 17:35
 */
class NurseConsultFragment : BaseFragment() {
    var recyclerView: RecyclerView? = null

    var refreshLayout: SmartRefreshLayout? = null
    var clEmpty: ConstraintLayout? = null

    var page = 1

    var mAdapter: NurseConsultAdapter? = null

    var data = ArrayList<FindPatientOrdersResponse.RecordsBean>()
    private var mChooseDialog: ChooseDialog? = null
    private var mEvaluateDialog: ConsultEvaluateDialog? = null


    override fun getLayoutResource(): Int {
        return R.layout.fragment_nurse_consult
    }

    override fun initView() {

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        recyclerView = rootView.findViewById<RecyclerView>(R.id.recyclerView)
        refreshLayout = rootView.findViewById<SmartRefreshLayout>(R.id.refreshLayout)
        clEmpty = rootView.findViewById<ConstraintLayout>(R.id.cl_empty)
        val manager = LinearLayoutManager(activity)
        manager.orientation = LinearLayoutManager.VERTICAL
        recyclerView?.layoutManager = manager
        mAdapter = NurseConsultAdapter()
        recyclerView?.adapter = mAdapter

        recyclerView?.adapter = mAdapter
        refreshLayout?.isEnableLoadMore = false
        refreshLayout?.setOnRefreshListener { refreshLayout: RefreshLayout? ->
            page = 1
            findPatientOrders()
        }
        refreshLayout?.setOnLoadMoreListener { refreshLayout: RefreshLayout? ->
            page++
            findPatientOrders()
        }


        mAdapter?.addChildClickViewIds(R.id.tv_cancel, R.id.tv_pay, R.id.tv_evaluate)
        mAdapter?.setOnItemChildClickListener { adapter: BaseQuickAdapter<*, *>?, view: View, position: Int ->
            when (view.id) {
                R.id.tv_cancel -> showCancelDialog(data[position].diagnoseId!!)

                R.id.tv_evaluate -> showEvaluateDialog(data[position].orderId!!)

            }
        }
    }

    //取消订单
    private fun cancleNurseConsult(diagnoseId: String? = "") {
        if (TextUtils.isEmpty(diagnoseId)) return
        RestProxy.getInstance().cancleNurseConsult(diagnoseId, object : BaseObserver<String?>() {
            override fun onSuccess(result: String?) {
                findPatientOrders()
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                ToastUtil.showToast(mContext, e.message)
            }
        })
    }

    override fun initData() {
    }

    /**
     * 取消订单 弹框
     */
    private fun showCancelDialog(diagnoseId: String? = "") {
        if (TextUtils.isEmpty(diagnoseId)) return
        mChooseDialog = ChooseDialog.newInstance()
        mChooseDialog?.show(childFragmentManager, "mDialog")
        mChooseDialog?.setText("是否取消订单", "确定", "取消")
        mChooseDialog?.setmListener(object : ChooseDialog.OnItemClickListener {
            override fun onConfirm() {
                cancleNurseConsult(diagnoseId)
            }

            override fun onCancel() {}
        })
    }

    /**
     * 评价弹框
     */
    private fun showEvaluateDialog(appointmentId: String) {
        mEvaluateDialog = ConsultEvaluateDialog.newInstance(appointmentId)
        mEvaluateDialog?.show(childFragmentManager, "EvaluateDialog")
        mEvaluateDialog?.setmListener(object : ConsultEvaluateDialog.OnItemClickListener {
            override fun onConfirm() {
                findPatientOrders()
            }

            override fun onCancel() {}
        })
    }

    override fun onResume() {
        super.onResume()
        findPatientOrders()
    }

    fun findPatientOrders() {

        RestProxy.getInstance()
            .findPatientOrders(page, 100, object : BaseObserver<FindPatientOrdersResponse>() {
                override fun onSuccess(result: FindPatientOrdersResponse?) {
                    if (result?.records == null || result.records!!.size == 0) {
                        clEmpty?.visibility = View.VISIBLE
                        refreshLayout?.visibility = View.GONE
                    } else {
                        clEmpty?.visibility = View.GONE
                        refreshLayout?.visibility = View.VISIBLE
                        if (page == 1) {
                            data.clear()
                            data.addAll(result.records!!)
                            refreshLayout?.finishRefresh()
                        } else {
                            data.addAll(result.records!!)
                            refreshLayout?.finishLoadMore()
                        }
                        mAdapter!!.setList(data)
                    }
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    ToastUtil.showToast(mContext, e.message)
                }
            })
    }
}